What is uniqs?
The uniqs npm package is a simple utility that allows you to filter out duplicate values from an array. It is particularly useful when you need to ensure that a list contains only unique items.
What are uniqs's main functionalities?
Removing duplicates from an array
This feature allows you to pass an array to the uniqs function, and it returns a new array with all the duplicate values removed, leaving only unique items.
[...new Set([1, 2, 2, 3, 4, 4, 5])] // returns [1, 2, 3, 4, 5]
Other packages similar to uniqs
lodash.uniq
lodash.uniq is a method from the popular Lodash library that produces a duplicate-free version of an array. It is similar to uniqs but comes as part of a larger utility library, which might be more suitable for projects that require a wide range of utility functions.
underscore
Underscore.js is a utility library that contains a uniq function, which is similar to uniqs. It offers a broader set of utility functions for manipulating and working with data. Unlike uniqs, which is focused solely on array deduplication, Underscore provides a more extensive toolkit for various operations.
array-unique
array-unique is another npm package that removes duplicate values from an array. It is similar to uniqs in functionality but may have different performance characteristics or implementation details.
Tiny utility to create unions and de-duplicated lists.
Example:
var uniqs = require('uniqs');
var foo = { foo: 23 };
var list = [3, 2, 2, 1, foo, foo];
uniqs(list);
You can pass multiple lists to create a union:
uniqs([2, 1, 1], [2, 3, 3, 4], [4, 3, 2]);
Passing individual items works too:
uniqs(3, 2, 2, [1, 1, 2]);
Summary
- Uniqueness is defined based on strict object equality.
- The lists do not need to be sorted.
- The resulting array contains the items in the order of their first appearance.
About
This package has been written to accompany utilities like
flatten as alternative to full-blown
libraries like underscore or lodash.
The implementation is optimized for simplicity rather than performance and
looks like this:
module.exports = function uniqs() {
var list = Array.prototype.concat.apply([], arguments);
return list.filter(function(item, i) {
return i == list.indexOf(item);
});
};
License
MIT